<template>
  <!-- 增加修改 -->
  <a-drawer width="50%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-row :gutter="24">
        <a-col
          :span="12">
          <a-form-model-item label="项目名称" prop="projectId">
            <a-tree-select
              v-model="form.projectId"
              style="width: 100%"
              :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
              :tree-data="deptOptions"
              placeholder="请选择"
              :replaceFields="replaceFields"
              tree-default-expand-all
              disabled
            >
            </a-tree-select>
          </a-form-model-item>
        </a-col>
        <a-col
          :span="12">
          <a-form-model-item label="内部批次号" prop="batchNumber">
            <a-input v-model="form.batchNumber" placeholder="请输入" />
          </a-form-model-item>
        </a-col>
      </a-row>

      <a-row :gutter="24">
        <a-col
          :span="12">
          <a-form-model-item label="零件图号" prop="partDrawingNo">
            <a-input v-model="form.partDrawingNo" placeholder="请输入"/>
          </a-form-model-item>
        </a-col>
        <a-col
          :span="12">
          <a-form-model-item label="零件名称" prop="partName">
            <a-input v-model="form.partName" placeholder="请输入" />
          </a-form-model-item>

        </a-col>
      </a-row>

      <a-row :gutter="24">
        <a-col
          :span="12">
          <a-form-model-item label="预计数量" prop="quantity">
            <a-input v-model="form.quantity" placeholder="请输入" />
          </a-form-model-item>
        </a-col>
        <a-col
          :span="12">
          <a-form-model-item label="计划来料日期" prop="planArrivalDate">
            <a-date-picker v-model="form.planArrivalDate" :format="'YYYY-MM-DD'" :value-format="'YYYY-MM-DD HH:mm:ss'"/>
          </a-form-model-item>
        </a-col>
      </a-row>

      <a-row :gutter="24">
        <a-col
          :span="12">
          <a-form-model-item label="计划交付日期" prop="planDeliveryDate">
            <a-date-picker v-model="form.planDeliveryDate" :format="'YYYY-MM-DD'" :value-format="'YYYY-MM-DD HH:mm:ss'"/>
          </a-form-model-item>
        </a-col>

      </a-row>

      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>

import { addPartPlanIncoming, partPlanIncoming, updatePartPlanIncoming } from '@/api/plan/incoming'

export default {
  name: 'CreateForm',
  props: {
    deptOptions: {
      type: Array,
      required: true
    }
  },
  components: {
  },
  data () {
    return {
      submitLoading: false,
      replaceFields: { children: 'children', title: 'projectName', key: 'id', value: 'id' },
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      formTitle: '',
      // 表单参数
      form: {
        userId: undefined,
        batchNumber: 0,
        quantity: undefined,
        projectName: undefined,
        planArrivalDate: undefined,
        partDrawingNo: undefined,
        partName: undefined,
        sex: '3',
        status: '0',
        remark: undefined,
        postIds: [],
        roleIds: []
      },
      open: false,
      rules: {
        quantity: [
          { required: true, message: '来料数量不能为空', trigger: 'blur' }
        ],
        projectName: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' }
        ],
        batchNumber: [
          { required: true, message: '外部批次号不能为空', trigger: 'change' }
        ],
        planArrivalDate: [
          { required: true, message: '计划来料时间不能为空', trigger: 'blur' }
        ],
        partName: [
          { required: true, message: '零件名称不能为空', trigger: 'blur' }
        ],
        partDrawingNo: [
          { required: true, message: '零件图号不能为空', trigger: 'blur' }
        ],
        planDeliveryDate: [
          { required: true, message: '计划交付日期不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  filters: {
  },
  created () {

  },
  computed: {
  },
  watch: {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        userId: undefined,
        batchNumber: undefined,
        quantity: undefined,
        projectName: undefined,
        planArrivalDate: undefined,
        partDrawingNo: undefined,
        partName: undefined
      }
    },
     /** 新增按钮操作 */
    handleAdd (projectId) {
      if (!projectId) {
        this.$message.error(
                '请先选择项目',
                3
              )
        return
      }
      this.reset()
      this.$emit('select-tree')
      this.open = true
      this.formTitle = '毛坏件来料计划'
      this.form.projectId = projectId
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      this.$emit('select-tree')
      const id = row.id
      partPlanIncoming(id).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改来料计划'
        this.form.planArrivalDate = ''
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined) {
            updatePartPlanIncoming(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addPartPlanIncoming(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
